<?php
namespace app\common\process;

class OrderRefund extends OrderRefundIpl {
    protected $request = null;
    protected $append = ['order_refund_log', 'order_refund_product_snapshot'];

    //获取request
    public function getRequest() {
        if($this->request == null) {
            $this->request = loader()->loadAny('request', 'jy\\OrderRefund');
        }
        return $this->request;
    }

    //申请退款
    public function refund() {
    	$validate = $this->validate;
        $model = $this->model;

    	$post = $this->getRequest()->getRefund();

    	if(!$validate->scene('refund')->check($post)) {
    		$this->error($validate->getError());
    	}
    	try { 
	    	$model->startTrans();
	    	$this->refund_ipl($post);
	    	$model->commit();
    	} catch(\Exception $e) {
    		$model->rollback();
            $this->error($e->getMessage());
    	}
    	return $this->success('保存成功');
    }

    //店铺获取退款单列表
    public function store_private_list() {
        $post = $this->getRequest()->getStorePrivateList();

        $model = $this->model;
        $validate = $this->validate;

        if (!$validate->scene('store')->check($post)) {
            $this->error($validate->getError());
        }

        //通过查询条件生成器生成查询条件
        loader()->loadAny('search', 'jy\\OrderRefund')->store_private_list($model);
        $model->append($this->append);

        if($post['limit']) {
            $list = $model->paginate($post['limit']);
        } else {
            $list = $model->select();
        }
        $this->assign('list', $list);
        return $this->fetch();
    }

    //用户查看退款单
    public function user_refund_info() {
        $validate = $this->validate;
        $post = $this->getRequest()->getUserInfo();

        if(!$validate->scene('user_info')->check($post)) {
            $this->error($validate->getError());
        }
        return $this->info_ipl($post['id']);
    }

    //用户查看退款单详情
    public function user_refund_all_info() {
        $validate = $this->validate;
        $post = $this->getRequest()->getUserInfo();

        if(!$validate->scene('user_info')->check($post)) {
            $this->error($validate->getError());
        }
        return $this->info_all_ipl($post['id']);
    }

    //商家查看退款单
    public function store_refund_info() {
        $validate = $this->validate;
        $post = $this->getRequest()->getStoreInfo();

        if(!$validate->scene('store_info')->check($post)) {
            $this->error($validate->getError());
        }
        return $this->info_ipl($post['id']);
    }

    //商家查看退款单详情
    public function store_refund_all_info() {
        $validate = $this->validate;
        $post = $this->getRequest()->getStoreInfo();

        if(!$validate->scene('store_info')->check($post)) {
            $this->error($e->getError());
        }
        return $this->info_all_ipl($post['id']);
    }

    //店铺操作退款申请
    public function store_refund_handle() {
        $validate = $this->validate;
        $model = $this->model;

        $post = $this->getRequest()->getStoreRefundHandle();

        if(!$validate->scene('store_handle')->check($post)) {
            $this->error($validate->getError());
        }
        $fun = 'store_refund_handle_' . $post['operate'] . '_ipl';

        try {
            $model->startTrans();
            $this->trigger('before_store_refund_handle_' . $post['operate']);
            $result = $this->$fun($post);
            $this->trigger('after_store_refund_handle_' . $post['operate']);
            $model->commit();
        } catch(\Exception $e) {
            $model->rollback();
            $this->error($e->getMessage());
        }
        return $result;
    }

    //买家退货全部处理
    public function user_return_product_all() {
        $id = $this->getRequest()->get_id();
        $refund = loadOnly('jy\\OrderRefund')->find('jy\\OrderRefund', $id);
        if($refund['status'] == loadEnum('OrderRefundStatus')->code2key('dengdaimaijiatuihuo2')) {
            return $this->user_return_product2();
        } else {
            return $this->user_return_product();
        }
    }

    //买家退货
    public function user_return_product() {
        $validate = $this->validate;
        $model = $this->model;

        $post = $this->getRequest()->getUserReturnProduct();

        if(!$validate->scene('user_return_product')->check($post)) {
            $this->error($validate->getError());
        }
        try {
            $model->startTrans();
            $this->trigger('before_user_return_product');
            $result = $this->user_return_product_ipl($post);
            $this->trigger('after_user_return_product');
            $model->commit();
        } catch(\Exception $e) {
            $model->rollback();
            throw $e;
            $this->error($e->getMessage());
        }
    }

    //商家收到退货产品全部处理
    public function store_received_return_all() {
        $id = $this->getRequest()->get_id();
        $refund = loadOnly('jy\\OrderRefund')->find('jy\\OrderRefund', $id);
        if($refund['status'] == loadEnum('OrderRefundStatus')->code2key('tuihuozhong2')) {
            return $this->store_received_return2();
        } else {
            return $this->store_received_return();
        }
    }

    //商家收到退货产品
    public function store_received_return() {
        $validate = $this->validate;
        $model = $this->model;

        $post = $this->getRequest()->getStoreReceivedReturn();

        if(!$validate->scene('store_received_return')->check($post)) {
            $this->error($validate->getError());
        }

        try {
            $model->startTrans();
            $this->trigger('before_store_received_return');
            $result = $this->store_received_return_ipl($post);
            $this->trigger('after_store_received_return');
            $model->commit();
        } catch(\Exception $e) {
            $model->rollback();
            $this->error($e->getMessage());
        }
    }

    //商家收到退货后操作全部处理
    public function store_refund_product_handle_all() {
        $id = $this->getRequest()->get_id();
        $refund = loadOnly('jy\\OrderRefund')->find('jy\\OrderRefund', $id);
        if($refund['status'] == loadEnum('OrderRefundStatus')->code2key('maijiayiqianshou2')) {
            return $this->store_refund_product2_handle();
        } else {
            return $this->store_refund_product_handle();
        }
    }

    //商家收到退货后操作
    public function store_refund_product_handle() {
        $validate = $this->validate;
        $model = $this->model;

        $post = $this->getRequest()->getStoreRefundProductHandle();

        if(!$validate->scene('store_refund_product_handle')->check($post)){
            $this->error($validate->getError());
        }

        $fun = 'store_refund_product_handle_' . $post['operate'] . '_ipl';

        try {
            $model->startTrans();
            $this->trigger('before_store_refund_product_handle_' . $post['operate']);
            $result = $this->$fun($post);
            $this->trigger('after_store_refund_product_handle_' . $post['operate']);
            $model->commit();
        } catch(\Exception $e) {
            $model->rollback();
            $this->error($e->getMessage());
        }
    }

    //用户申请管理员介入全部处理
    public function apply_for_administrator_intervention_all() {
        $id = $this->getRequest()->get_id();
        $refund = loadOnly('jy\\OrderRefund')->find('jy\\OrderRefund', $id);
        if($refund['status'] == loadEnum('OrderRefundStatus')->code2key('tuihuomaijiajujue') || $refund['status'] == loadEnum('OrderRefundStatus')->code2key('tuihuomaijiajujue2')) {
            return $this->apply_for_administrator_intervention2();
        } else {
            return $this->apply_for_administrator_intervention();
        }
    }

    //用户申请管理员介入
    public function apply_for_administrator_intervention() {
        $validate = $this->validate;
        $model = $this->model;

        $post = $this->getRequest()->getApplyForAdministratorIntervention();

        if(!$validate->scene('apply_for_administrator_intervention')->check($post)){
            $this->error($validate->getError());
        }

        try {
            $model->startTrans();
            $this->trigger('before_apply_for_administrator_intervention');
            $result = $this->apply_for_administrator_intervention_ipl($post);
            $this->trigger('after_apply_for_administrator_intervention');
            $model->commit();
        } catch(\Exception $e) {
            $model->rollback();
            $this->error($e->getMessage());
        }
    }

    //管理员操作全部处理
    public function administrator_hanlde_all() {
        $id = $this->getRequest()->get_id();
        $refund = loadOnly('jy\\OrderRefund')->find('jy\\OrderRefund', $id);
        if($refund['status'] == loadEnum('OrderRefundStatus')->code2key('shenqingguanliyuanjieru2')) {
            return $this->administrator_hanlde2();
        } else {
            return $this->administrator_hanlde();
        }
    }

    //管理员操作
    public function administrator_hanlde() {
        $validate = $this->validate;
        $model = $this->model;

        $post = $this->getRequest()->getAdministratorHandle();

        if(!$validate->scene('administrator_hanlde')->check($post)){
            $this->error($validate->getError());
        }

        $fun = 'administrator_hanlde_' . $post['operate'] . '_ipl';

        try {
            $model->startTrans();
            $this->trigger('before_administrator_hanlde_' . $post['operate']);
            $result = $this->$fun($post);
            $this->trigger('after_administrator_hanlde_' . $post['operate']);
            $model->commit();
        } catch(\Exception $e) {
            $model->rollback();
            $this->error($e->getMessage());
        }
    }

    //管理员同意退款后买家退货
    public function user_return_product2() {
        $validate = $this->validate;
        $model = $this->model;

        $post = $this->getRequest()->getUserReturnProduct2();

        if(!$validate->scene('user_return_product2')->check($post)) {
            $this->error($validate->getError());
        }
        try {
            $model->startTrans();
            $this->trigger('before_user_return_product2');
            $result = $this->user_return_product2_ipl($post);
            $this->trigger('after_user_return_product2');
            $model->commit();
        } catch(\Exception $e) {
            $model->rollback();
            $this->error($e->getMessage());
        }
    }

    //管理员同意退款后商家收到买家退货
    public function store_received_return2() {
        $validate = $this->validate;
        $model = $this->model;

        $post = $this->getRequest()->getStoreReceivedReturn2();

        if(!$validate->scene('store_received_return2')->check($post)) {
            $this->error($validate->getError());
        }

        try {
            $model->startTrans();
            $this->trigger('before_store_received_return2');
            $result = $this->store_received_return2_ipl($post);
            $this->trigger('after_store_received_return2');
            $model->commit();
        } catch(\Exception $e) {
            $model->rollback();
            throw $e;
            $this->error($e->getMessage());
        }
    }

    //管理员同意退款后商家收到退货后操作
    public function store_refund_product2_handle() {
        $validate = $this->validate;
        $model = $this->model;

        $post = $this->getRequest()->getStoreRefundProduct2Handle();

        if(!$validate->scene('store_refund_product2_handle')->check($post)){
            $this->error($validate->getError());
        }

        $fun = 'store_refund_product2_handle_' . $post['operate'] . '_ipl';

        try {
            $model->startTrans();
            $this->trigger('before_store_refund_product2_handle_' . $post['operate']);
            $result = $this->$fun($post);
            $this->trigger('after_store_refund_product2_handle_' . $post['operate']);
            $model->commit();
        } catch(\Exception $e) {
            $model->rollback();
            $this->error($e->getMessage());
        }
    }

    //申请管理员介入
    public function apply_for_administrator_intervention2() {
        $validate = $this->validate;
        $model = $this->model;

        $post = $this->getRequest()->getApplyForAdministratorIntervention2();

        if(!$validate->scene('apply_for_administrator_intervention2')->check($post)){
            $this->error($validate->getError());
        }

        try {
            $model->startTrans();
            $this->trigger('before_apply_for_administrator_intervention2');
            $result = $this->apply_for_administrator_intervention2_ipl($post);
            $this->trigger('after_apply_for_administrator_intervention2');
            $model->commit();
        } catch(\Exception $e) {
            $model->rollback();
            $this->error($e->getMessage());
        }
    }

    //管理员再次操作
    public function administrator_hanlde2() {
        $validate = $this->validate;
        $model = $this->model;

        $post = $this->getRequest()->getAdministratorHandle2();

        if(!$validate->scene('administrator_hanlde2')->check($post)){
            $this->error($validate->getError());
        }

        $fun = 'administrator_hanlde2_' . $post['operate'] . '_ipl';

        try {
            $model->startTrans();
            $this->trigger('before_administrator_hanlde2_' . $post['operate']);
            $result = $this->$fun($post);
            $this->trigger('after_administrator_hanlde2_' . $post['operate']);
            $model->commit();
        } catch(\Exception $e) {
            $model->rollback();
            $this->error($e->getMessage());
        }
    }
}
